<template>
  <!-- 列表 align="center" -->
  <ContentWrap class="mt-10px">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
              :row-class-name="rowClassName">

      <el-table-column label="客户道具编码" align="center" prop="itemNo" width="140">
        <template #default="scope">
          <span :style="{ color: getColor(scope.row.productStatus) }">{{ scope.row.itemNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="no" width="140"/>
      <el-table-column label="产品名称" align="center" prop="productName" width="240"/>
      <el-table-column label="产品规格尺寸" align="center" prop="dimension" width="160"/>
      <el-table-column label="产品状态" align="center" prop="productStatus" width="160">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_PRODUCT_STATUS" :value="scope.row.productStatus" />
        </template>
      </el-table-column>
      <el-table-column label="标配系数" align="center" prop="coefficient" />
      <el-table-column label="当前库存数量" align="center" prop="stockNum" />
      <el-table-column label="客户备货数量" align="center" prop="customerStockCount" />
      <el-table-column label="理论备货数量" align="center" prop="theoryStockCount" />
      <el-table-column label="实际备货数量" align="center" prop="stockCount" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column v-if="stock.stockStatus === 0" label="操作" align="center" fixed="right" width="160">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="editForm(scope.row.id)"
            v-hasPermi="['crm:product:update']"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加 -->
  <StockProductDetailForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import StockProductDetailForm from '../../stockproductdetail/StockProductDetailForm.vue'
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'
import {StockProductDetailApi} from "@/api/crm/stockproductdetail";
import { StockProductApi, StockProduct } from '@/api/crm/stockproduct'

defineOptions({ name: 'CrmStockProductList' })
const props = defineProps<{
  bizType: number // 业务类型
  bizId: number // 业务编号
  customerId: number // 业务编号
  stock: StockProduct
}>()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const customerId = computed(() => {
  return props.customerId;
});
const id = computed(() => {
  return props.bizId;
});

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  stockId: undefined as unknown // 允许 undefined + number
})
const rowClassName = ({ row, rowIndex }) => {
  if (row.productStatus === '1' && rowIndex===1) {
    return 'row-color-a';
  }
  return 'row-color-a'; // 默认返回空字符串，不应用任何特殊样式
}
const  getColor = (status)=>{
  switch (status) {
    case 2:
      // 未提交
      return "#FFB009";
      break;
    default:
      return "";
      break;
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 置空参数
    queryParams.stockId = undefined
    queryParams.stockId = props.bizId
    // 执行查询
    let data = { list: [], total: 0 }
    data = await StockProductDetailApi.getStockProductDetailPage(queryParams)
    if(data){
      list.value = data.list
      total.value = data.total
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 查看 */
const formRef = ref()

const editForm = (id) => {
  formRef.value.open('update',id)
}

/** 监听打开的 bizId + bizType，从而加载最新的列表 */
watch(
  () => [props.bizId, props.bizType],
  () => {
    handleQuery()
  },
  { immediate: true, deep: true }
)
</script>

<style>
.row-color-a {
  background-color: #6ae39e; /* 例如，类型A的行背景色 */
}
.row-color-b {
  background-color: #ebf4fa; /* 例如，类型B的行背景色 */
}
</style>
